package com.huiyujinrong.qiangdanbao.activity.fragment;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.huiyujinrong.qiangdanbao.R;
import com.huiyujinrong.qiangdanbao.city.CityPicker;
import com.huiyujinrong.qiangdanbao.city.dataPicker.CustomPickerDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 个人贷款
 */
public class FragmentPersonal extends Fragment {

    private static final int GE_REN = 1;


    @BindView(R.id.personal_YongTu)
    TextView YongTu;  //贷款用途
    @BindView(R.id.personal_ChengShi)
    TextView ChengShi;  //工作城市
    @BindView(R.id.personal_EDu)
    TextView EDu;   //贷款额度
    @BindView(R.id.personal_QiXian)
    TextView QiXian;    //贷款期限
    @BindView(R.id.personal_SheBao)
    TextView SheBao;    //社保缴纳
    @BindView(R.id.personal_GongJiJin)
    TextView GongJiJin; //公积金缴纳

    private List<String> selectData;
    private Dialog chooseDialog;
    private int perNum = 1;
    private String[] data;

    private int setNum;


    private String yongtu="",chengshi="",edu="",qixian="",shebao="",gongjijin="";

    private int user_type=1;  //工薪族

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_personal, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        user_type=1;
    }

    /**
     * 点击选择
     */
    @OnClick({R.id.personal_YongTu,
            R.id.personal_ChengShi,
            R.id.personal_EDu,
            R.id.personal_QiXian,
            R.id.personal_SheBao,
            R.id.personal_GongJiJin})
    void selectData(View view) {
        switch (view.getId()) {
            case R.id.personal_YongTu:
                perNum = 1;
                data = getResources().getStringArray(R.array.YongTu);
                break;
            case R.id.personal_ChengShi:
                perNum = 2;
                setCity();
                break;
            case R.id.personal_EDu:
                perNum = 3;
                setNum=3;
                data = getResources().getStringArray(R.array.EDu);
                break;
            case R.id.personal_QiXian:
                perNum = 4;
                setNum=4;
                data = getResources().getStringArray(R.array.QiXian);
                break;
            case R.id.personal_SheBao:
                perNum = 5;
                setNum=5;
                data = getResources().getStringArray(R.array.SheBao);
                break;
            case R.id.personal_GongJiJin:
                perNum = 6;
                setNum=6;
                data = getResources().getStringArray(R.array.GongJiJin);
                break;
        }
        if (perNum != 2) {
            setSelectData(data);
        }

    }

    //添加选择数据
    private void setSelectData(String[] data) {
        selectData = new ArrayList<>();
        for (String str : data) {
            selectData.add(str);
        }
        showSelectData(selectData);
    }

    //选择  确定操作
    private void showSelectData(List<String> mlist) {
        CustomPickerDialog.Builder builder = new CustomPickerDialog.Builder(getActivity());
        chooseDialog = builder.setData(mlist).setSelection(0).setTitle("取消")
                .setOnDataSelectedListener(new CustomPickerDialog.OnDataSelectedListener() {
                    @Override
                    public void onDataSelected(String itemValue, int position) {
                        setShowDataItem(perNum, itemValue);
                        switch (setNum){
                            case 3:
                                switch (position){
                                    case 0:
                                        edu="";
                                        break;
                                    case 1:
                                        edu="0,1";
                                        break;
                                    case 2:
                                        edu="1,5";
                                        break;
                                    case 3:
                                        edu="5,10";
                                        break;
                                    case 4:
                                        edu="10,20";
                                        break;
                                    case 5:
                                        edu="20,10000";
                                        break;
                                }
                                break;
                            case 4:
                                switch (position){
                                    case 0:
                                        qixian="";
                                        break;
                                    case 1:
                                        qixian="1,12";
                                        break;
                                    case 2:
                                        qixian="12,24";
                                        break;
                                    case 3:
                                        qixian="24,36";
                                        break;
                                }
                                break;
                            case 5:
                                switch (position){
                                    case 0:
                                        shebao="";
                                        break;
                                    case 1:
                                        shebao="1";
                                        break;
                                }
                                break;
                            case 6:
                                switch (position){
                                    case 0:
                                        gongjijin="";
                                        break;
                                    case 1:
                                        gongjijin="1";
                                        break;
                                }
                                break;
                        }
                    }

                    @Override
                    public void onCancel() {

                    }
                }).create();
        chooseDialog.show();
    }


    //设置 选择内容
    private void setShowDataItem(int perNum, String itemValue) {
        switch (perNum) {
            case 1:
                YongTu.setText(itemValue);
                break;
            case 3:
                EDu.setText(itemValue);
                break;
            case 4:
                QiXian.setText(itemValue);
                break;
            case 5:
                SheBao.setText(itemValue);
                break;
            case 6:
                GongJiJin.setText(itemValue);
                break;
        }
    }

    //选择城市
    private void setCity() {
        CityPicker cp = new CityPicker.Builder(getActivity())
                .city("全部")
                .province("城市")
                .provinceCyclic(false)
                .textColor(Color.parseColor("#696969"))
                .cityCyclic(false)
                .districtCyclic(false)
                .visibleItemsCount(7)
                .onlyShowProvinceAndCity(true)
                .itemPadding(10)
                .build();
        cp.show();

        cp.setOnCityItemClickListener(new CityPicker.OnCityItemClickListener() {
            @Override
            public void onSelected(String... citySelected) {
                //顺序 省市""
                String data = String.format("%s-%s", citySelected[0], citySelected[1]);
                if (citySelected[0].contains("全部")) {
                    ChengShi.setText("全部-城市");
                    chengshi="";
                } else {
                    ChengShi.setText(data);
                    chengshi=citySelected[2];
                }
            }

            @Override
            public void onCancel() {

            }
        });
    }

    //重置
    @OnClick(R.id.personal_reset)
    void reSet() {
        YongTu.setText(R.string.pl_select);
        ChengShi.setText(R.string.pl_select);
        EDu.setText(R.string.pl_select);
        QiXian.setText(R.string.pl_select);
        SheBao.setText(R.string.pl_select);
        GongJiJin.setText(R.string.pl_select);
    }

    //确定
    @OnClick(R.id.personal_determine)
    void determine() {

        if(YongTu.getText().toString().equals("请选择")){
            yongtu="";
        }else {
            if(YongTu.getText().toString().equals("用途不限")){
                yongtu="";
            }else {
                yongtu = YongTu.getText().toString();
            }
        }

        if(EDu.getText().toString().equals("请选择")){
            edu="";
        }

        if(QiXian.getText().toString().equals("请选择")){
            qixian="";
        }

        if(SheBao.getText().toString().equals("请选择")){
            shebao="";
        }

        if(GongJiJin.getText().toString().equals("请选择")){
            gongjijin="";
        }


        Bundle bundle = new Bundle();
        bundle.putString("YongTu", yongtu);
        bundle.putString("ChengShi", chengshi);
        bundle.putString("EDu", edu);
        bundle.putString("QiXian", qixian);
        bundle.putInt("user_type",user_type);

        bundle.putString("SheBao", shebao);
        bundle.putString("GongJiJin", gongjijin);

        Intent intent = new Intent();
        intent.putExtras(bundle);


        getActivity().setResult(Activity.RESULT_OK,intent);
        getActivity().finish();
    }
}
